<template>
  <div class="pokemon-console-container">
    <div class="pokemon-list">
      <div class="pokemon-list-header">
        <div class="header-title">
          宝可梦控制台 {{ capturedPokemonStore.viewList.length }}/{{
            maxPokemon
          }}
        </div>
        <div class="ability-icons-container">
          <div class="ability-icons">
            <div
              class="ability-icon"
              v-for="ability in abilitiesList"
              :key="ability.type"
              :title="ability.name"
            >
              {{ ability.icon }}
            </div>
          </div>
        </div>
      </div>
      <div
        class="pokemon-item"
        v-for="pokemon in pokemonList"
        :key="pokemon.captureId"
      >
        <div class="pokemon-info">
          <div class="pokemon-avatar">
            <img :src="getPokemon(pokemon.id, 'icon')" class="pokemon-img" />
          </div>
          <div class="pokemon-name">{{ getPokemon(pokemon.id, 'name') }}</div>
        </div>
        <div class="pokemon-abilities">
          <div
            class="ability-checkbox"
            v-for="ability in abilitiesList"
            :key="ability.type"
          >
            <input
              type="checkbox"
              :id="`${pokemon.captureId}-${ability.type}`"
              :checked="isPokemonWorkingOn(pokemon, ability.type)"
              @change="toggleAbility(pokemon, ability.type)"
              :disabled="!hasPokemonAbility(pokemon.id, ability.type)"
            />
            <label
              :for="`${pokemon.captureId}-${ability.type}`"
              :class="{
                disabled: !hasPokemonAbility(pokemon.id, ability.type),
              }"
            ></label>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useCapturedPokemonStore } from '@/store/capturedPokemon'
import { usePokemonAllStore } from '@/store/pokemonAll'
import { useAbilitiesStore } from '@/store/abilities'
import { CapturedPokemon } from '@/types/pokemon'

const capturedPokemonStore = useCapturedPokemonStore()
const abilitiesStore = useAbilitiesStore()
const maxPokemon = ref(21) // 最大宝可梦数量，可以根据实际情况调整

// 宝可梦列表
const pokemonList = computed(() => {
  return capturedPokemonStore.viewList
})

// 能力列表
const abilitiesList = computed(() => {
  return abilitiesStore.abilitiesList
})

const getPokemon = (id: string, type: 'name' | 'icon' | 'themeColor') => {
  const pokemon = window.$poketmon.getDataById(id)
  if (!pokemon) return ''
  if (type === 'name') return pokemon.name
  if (type === 'icon') return pokemon.icon
  if (type === 'themeColor') return pokemon.uiView.themeColor
  return ''
}
const hasPokemonAbility = (pokemonId: string, abilityType: string) => {
  const pokemon = window.$poketmon.getDataById(pokemonId)
  if (!pokemon || !pokemon.abilities) return false
  return pokemon.abilities.some((ability) => ability.type === abilityType)
}

// 判断宝可梦是否正在进行某项工作
const isPokemonWorkingOn = (pokemon: CapturedPokemon, abilityType: string) => {
  return pokemon.currentWork.some((work) => work.type === abilityType)
}

// 选择
const toggleAbility = (pokemon: CapturedPokemon, abilityType: string) => {
  if (!pokemon) return
  if (!hasPokemonAbility(pokemon.id, abilityType)) return

  const workIndex = pokemon.currentWork.findIndex(
    (work) => work.type === abilityType
  )

  if (workIndex >= 0) {
    pokemon.currentWork.splice(workIndex, 1)
  } else {
    pokemon.currentWork.push({ type: abilityType as any, level: 1 })
  }
}
</script>

<style lang="less" scoped>
.pokemon-console-container {
  padding: 10px;
  color: #fff;
}

.pokemon-list {
  width: 100%;

  .pokemon-list-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid #ddd;
    margin-bottom: 5px;

    .header-title {
      font-size: 14px;
      font-weight: bold;
      width: 150px;
    }

    .ability-icons-container {
      flex: 1;
      display: flex;

      .ability-icons {
        display: flex;
        flex: 1;
        justify-content: space-around;

        .ability-icon {
          font-size: 16px;
          width: 24px;
          height: 24px;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }

  .pokemon-item {
    display: flex;
    align-items: center;
    padding: 5px 0;
    border-bottom: 1px solid #eee;

    .pokemon-info {
      display: flex;
      align-items: center;
      width: 150px; /* 与标题区域宽度一致 */

      .pokemon-avatar {
        position: relative;
        width: 32px;
        height: 32px;
        border-radius: 50%;
        margin-right: 8px;
        display: flex;
        justify-content: center;
        align-items: center;

        .level {
          position: absolute;
          top: -3px;
          left: -3px;
          background-color: rgba(0, 0, 0, 0.7);
          border-radius: 8px;
          padding: 1px 3px;
          font-size: 9px;
          color: white;
        }

        .pokemon-img {
          width: 100%;
          height: 100%;
          object-fit: contain;
        }
      }

      .pokemon-name {
        font-size: 13px;
        font-weight: bold;
      }
    }

    .pokemon-abilities {
      display: flex;
      flex: 1;
      justify-content: space-around;

      .ability-checkbox {
        width: 24px;
        height: 24px;
        position: relative;

        input[type='checkbox'] {
          opacity: 0;
          width: 0;
          height: 0;

          &:checked + label {
            &:after {
              content: '✓';
              position: absolute;
              top: 50%;
              left: 50%;
              transform: translate(-50%, -50%);
              color: #1976d2; /* 蓝色勾选标记 */
              font-size: 14px;
              font-weight: bold;
            }
          }

          &:disabled + label {
            opacity: 0.5;
            cursor: not-allowed;
          }
        }

        label {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          border: 1px solid #555;
          cursor: pointer;
          background-color: #333;

          &:hover {
            background-color: #444;
          }

          &.disabled {
            opacity: 0.5;
            cursor: not-allowed;
            background-color: #222;
            border-color: #444;

            &:hover {
              background-color: #222;
            }
          }
        }
      }
    }
  }
}
</style>
